﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Common;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Entities.Enums;

namespace AutoPartsSearch.Domain.Concrete.Repositories
{
    public class DapperBlackListRepository : BaseRepository, IBlackListRepository
    {
        public DapperBlackListRepository(ILanguageGetter languageGetter) : base(languageGetter)
        {
        }

        public IEnumerable<BlackList> GetBySellerCode(Guid code, bool onlyApproved = true)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@SellerCode", code, DbType.Guid);
            param.Add("@OnlyApproved", onlyApproved, DbType.Boolean);
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            return QueryExecutor.QueryDynamic<BlackList>("sp_GetBlackListsBySellerCode", ModelReader, param);
        }

        public int Add(Entities.BlackList instance)
        {
            var id = -1;
            var param = new Dapper.DynamicParameters();
            param.Add("@Id", id, DbType.Int32, ParameterDirection.Output);
            param.Add("@Name", instance.Name, DbType.String);
            param.Add("@City", instance.City, DbType.String);
            param.Add("@EMail", instance.EMail, DbType.String);
            param.Add("@Phone", instance.Phone, DbType.String);
            param.Add("@Comments", instance.Comments, DbType.String);
            param.Add("@SellerId", instance.Seller.Id, DbType.Int32);
            QueryExecutor.ExecuteProcedure("sp_AddBlackList", param);
            id = param.Get<int>("@Id");
            return id;
        }

        public void Update(Entities.BlackList instance)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Id", instance.Id, DbType.Int32);
            param.Add("@Comments", instance.Comments, DbType.String);
            param.Add("@IsApproved", instance.IsApproved, DbType.Boolean);
            QueryExecutor.ExecuteProcedure("sp_UpdateBlackList", param);
        }

        public Entities.BlackList GetById(int id)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Id", id, DbType.Int32);
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            return QueryExecutor.QueryDynamic<BlackList>("sp_GetBlackListById", ModelReader, param).FirstOrDefault();
        }

        public IEnumerable<Entities.BlackList> Get()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<Entities.BlackList> Get(int page, int pageSize)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            param.Add("@Page", page, DbType.Int32);
            param.Add("@PageSize", pageSize, DbType.Int32);
            return QueryExecutor.QueryDynamic<BlackList>("sp_GetBlackListsPaged", ModelReader, param);
        }

        public IEnumerable<Entities.BlackList> GetApproved(int page, int pageSize)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            param.Add("@Page", page, DbType.Int32);
            param.Add("@PageSize", pageSize, DbType.Int32);
            return QueryExecutor.QueryDynamic<BlackList>("sp_GetBlackListsAprovedPaged", ModelReader, param);
        }

        public void Delete(int id)
        {
            throw new NotImplementedException();
        }

        public long GetTotalCount()
        {
            return QueryExecutor.QueryProcedure<int>("sp_GetBlackListTotalCount").FirstOrDefault();
        }

        private void ModelReader(BlackList blackList, dynamic o)
        {
            blackList.Id = o.Id;
            blackList.Name = o.Name;
            blackList.City = o.City;
            blackList.Comments = o.Comments;
            blackList.EMail = o.EMail;
            blackList.IsApproved = o.IsApproved;
            blackList.Phone = o.Phone;
            blackList.Seller = new Seller
                                   {
                                       Id = o.SellerId,
                                       FullName = o.SellerFullName,
                                       Adress = o.SellerAdress,
                                       City = new Dictionary(o.SellerCity),
                                       Code = o.SellerCode,
                                       CompanyName = o.SellerCompanyName,
                                       EMail = o.SellerEMail,
                                       IsBlocked = o.SellerIsBlocked
                                   };
        }
    }
}
